package com.auxgroup.smarthome.enterpriseapi.locations;

import com.alibaba.fastjson.JSONReader;

import java.io.FileNotFoundException;
import java.io.FileReader;

/**
 * Created by kevinchen on 2017/3/25.
 */
public class ReadLocationJson {

    public static LocationEnCnMaps readLocationJson(){
        LocationEnCnMaps locationEnCnMaps = new LocationEnCnMaps();
        try {
            JSONReader reader = new JSONReader(new FileReader(LocationJsonPathConstant.JSON_PATH));
            reader.startObject();
            while (reader.hasNext()){
                String key = reader.readString();
                if ("country".compareTo(key)==0){
                    reader.startObject();
                    while (reader.hasNext()){
                        String countryEn = reader.readString();
                        String countryCn= reader.readObject().toString();
                        locationEnCnMaps.putCountry(countryEn,countryCn);
                    }
                    reader.endObject();
                }
                else if ("province".compareTo(key)==0){
                    reader.startObject();
                    while (reader.hasNext()){
                        String provinceEn = reader.readString();
                        String provinceCn= reader.readObject().toString();
                        locationEnCnMaps.putRegion(provinceEn,provinceCn);
                    }
                    reader.endObject();
                }
                else if ("city".compareTo(key)==0){
                    reader.startObject();
                    while (reader.hasNext()){
                        String cityEn = reader.readString();
                        String cityCn= reader.readObject().toString();
                        locationEnCnMaps.putCity(cityEn,cityCn);
                    }
                    reader.endObject();
                }
            }
            reader.endObject();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return locationEnCnMaps;
    }

}
